<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>

<style>
.spacer {
  height: 1000px;
  width: 1000px;
}
#scroller {
  height: 100px;
  width: 100px;
  overflow: scroll;
}

</style>
<div id=scroller>
    <div class=spacer></div>
</div>
<div class=spacer></div>

<script>
// FIXME: Make this smaller. crbug.com/414283.
var floatPrecision = 0.01;

function testScroll(scrollOffset, expectedScrollOffset) {
  // Scrolling DIV with scrollTop/scrollLeft
  scroller.scrollTop = scrollOffset;
  scroller.scrollLeft = scrollOffset;
  assert_approx_equals(scroller.scrollTop, expectedScrollOffset, floatPrecision);
  assert_approx_equals(scroller.scrollLeft, expectedScrollOffset, floatPrecision);

  //Scrolling the document with window.scroll
  window.scroll(0,0);
  scrollOffset++;
  expectedScrollOffset++;
  window.scroll(scrollOffset, scrollOffset);
  assert_approx_equals(window.scrollY, expectedScrollOffset, floatPrecision);
  assert_approx_equals(window.scrollX, expectedScrollOffset, floatPrecision);

  // Scrolling the document with window.scrollTo
  window.scroll(0,0);
  window.scrollTo(scrollOffset, scrollOffset);
  assert_approx_equals(window.pageYOffset, expectedScrollOffset, floatPrecision);
  assert_approx_equals(window.pageXOffset, expectedScrollOffset, floatPrecision);

  // Scrolling the document with window.scrollBy
  window.scroll(1,1);
  window.scrollBy(scrollOffset - 1, scrollOffset - 1);
  assert_approx_equals(window.scrollY, expectedScrollOffset, floatPrecision);
  assert_approx_equals(window.scrollX, expectedScrollOffset, floatPrecision);
}

test( () => {
  const setOffset = 4.2;
  let expectedOffset = 4;

  // When fractional offsets are enabled, we should allow setting a fractional
  // scroll offset.
  if (internals.runtimeFlags.fractionalScrollOffsetsEnabled)
    expectedOffset = 4.2;

  testScroll(setOffset, expectedOffset);
}, "Set fractional scroll offset without zoom");

test( () => {
  testRunner.setPageZoomFactor(2);

  // Zooming provides additional granularity, regardless of the runtimeFlag
  // being enabled or not.
  const setOffset = 4.5;
  const expectedOffset = 4.5;
  testScroll(setOffset, expectedOffset);
}, "Set fractional scroll offset when zoomed");

test( () => {
  testRunner.setPageZoomFactor(2);

  // Zooming provides additional granularity but we try to set a value beyond the 0.5 increment.
  const setOffset = 4.6;
  let expectedOffset = 4.5;

  // When fractional offsets are enabled, we should allow setting a fractional
  // scroll offset.
  if (internals.runtimeFlags.fractionalScrollOffsetsEnabled)
    expectedOffset = 4.6;

  testScroll(setOffset, expectedOffset);
}, "Set fractional scroll offset to higher granularity when zoomed.");

</script>
